<?php

namespace app\admin\controller\subject;

use app\common\controller\Backend;

/**
 * 课程回收站管理
 * @icon fa fa-user
 */
class Subrecycle extends Backend
{
    // 快速查询，搜索指定的哪些字段
    protected $searchFields = 'subject.id,category.name,title,price';
    /**
     * 
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        // 课程模型
        $this->SubjectModel = model('Subject.Subject');
        $this->CategoryModel = model('Subject.Category');
        $this->ChapterModel = model('Subject.Chapter');
        // 查询所有的分类
        $list = $this->CategoryModel->select();

        $categorydata = [];

        foreach ($list as  $value) {
            $categorydata[$value['id']] = $value['name'];
        }
        // 并且渲染全局
        $this->view->assign('categorydata', $categorydata);
    }

    // 查看
    public function index()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        if ($this->request->isAjax()) {

            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }

            // 此处将筛选请求中有争议的字段"id"指定为主表中的id
            $this->changeWhereName('id', 'subject.id');

            list($where, $sort, $order, $offset, $limit) = $this->buildparams();

            $total = $this->SubjectModel
                ->onlyTrashed()
                ->with('category')
                ->where($where)
                ->order($sort, $order)
                ->limit($offset, $limit)
                ->count();  //查询总数

            $list = $this->SubjectModel
                ->onlyTrashed()
                ->with('category')
                ->where($where)
                ->order($sort, $order)
                ->limit($offset, $limit)
                ->select();  //查询数据

            $result = array("total" => $total, "rows" => $list);

            return json($result);
        }
        return $this->view->fetch();
    }

    // 删除
    public function del($ids = null)
    {
        // 课程ID
        $ids = empty($ids) ? [] : explode(',', $ids);

        // 章节ID
        $idlist = [];

        // 章节视频列表
        $urllist = [];

        // 课程图片列表
        $srclist = [];

        // 判断是否有没有当前数据  thumbs
        foreach ($ids as $item) {
            $res = $this->SubjectModel->onlyTrashed()->find($item);
            if (!$res) {
                $this->error('未找当前课程');
            }
            
            // 获取所课程的图片地址
            array_push($srclist, $res['thumbs']);

            // 获取所课程下的章节ID
            $chapterid = $this->ChapterModel->where('subid', $item)->column('id');
            // 合并多个数组
            $idlist = array_merge($idlist, $chapterid);

            // 课程章节下的所有URL视频链接
            $chapterurl = $this->ChapterModel->where('subid', $item)->column('url');
            $urllist = array_merge($urllist, $chapterurl);
        }
        // 课程图片地址
        $srclist = array_filter($srclist);
        // 章节视频地址
        $urllist = array_filter($urllist);


        $subjectstatus = $this->SubjectModel->destroy($ids,true);
        
        if ($subjectstatus === FALSE) {
            $this->error($this->SubjectModel->getError());
        }

        $chapterstatus = $this->ChapterModel->destroy($idlist);
        if ($chapterstatus === FALSE) {
            $this->error($this->ChapterModel->getError());
        }

        if ($subjectstatus === FALSE || $chapterstatus === FALSE) {
            $this->error('课程删除失败');
        } else {

            // 批量删除视频文件
            foreach ($urllist as $item) {
                $thumbs = substr($item, 1);
                @is_file($thumbs) && @unlink($thumbs);
            }

            // 批量删除课程图片
            foreach ($srclist as $val) {
                $src = substr($val, 1);
                @is_file($src) && @unlink($src);
            }

            $this->success('课程删除成功');
        }

    }

    // 还原
    public function reduction($ids = null)
    {
        $id = empty($ids) ? [] : explode(',', $ids);

        // 判断是否有没有当前数据
        foreach ($id as $item) {
            $res = $this->SubjectModel->onlyTrashed()->find($item);
            if (!$res) {
                $this->error('未找当前课程');
            }
        }

        $wheres = [
            'id' => ['in', $id]
        ];
        $result = $this->SubjectModel->onlyTrashed()->where($wheres)->update(['deletetime' => null]);

        if ($result) {
            $this->success('还原成功');
        } else {
            $this->error($this->SubjectModel->getError());
        }
    }
}
